package at.fhj.itm;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.widget.ProgressBar;

public class SplashActivity extends Activity {
	
	protected boolean active = true;
    protected int splashTime = 5000;
    private int waited = 0;
    private ProgressBar progressBar;
    private Handler mHandler = new Handler();
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        
        progressBar = (ProgressBar) findViewById(R.id.progressBar_main);
        
     // thread for displaying the SplashScreen
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    
                    while(active && (waited < splashTime)) {
                        sleep(100);
                        if(active) {
                            waited += 100;
                        }
                     // Update the progress bar
                        mHandler.post(new Runnable() {
                            public void run() {
                                progressBar.setProgress(waited);
                            }
                        });
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    finish();
                    startActivity(new Intent(SplashActivity.this,MainActivity.class));
                    stop();
                }
            }
        };
        splashTread.start();
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            active = false;
        }
        return true;
    }
        
}
